package com.nidu.demo.user.ability;

import cn.hutool.core.util.StrUtil;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.constants.Constants;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.user.gateway.UserDepartmentGateway;
import com.nidu.demo.user.gateway.UserPositionGateway;
import com.nidu.demo.user.gateway.UserGateway;
import com.nidu.demo.user.gateway.UserRoleGateway;
import com.nidu.demo.user.model.User;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
@RequiredArgsConstructor
public class UserDomainService {

    private final PasswordEncoder passwordEncoder;

    private final UserGateway userGateway;

    /**
     * 判断用户名是否重复
     * @param username 用户账号
     * @param mobile 电话号码
     * @param email 邮箱
     * @param excludeId 排除的Id
     */
    public void validateDuplicate(String username, String mobile, String email, Long excludeId) {
        // 检查用户名是否重复
        User user = userGateway.getByUsername(username);
        if(user != null){
            if(excludeId == null){
                throw new BizException(ErrorCodeConstants.USER_NAME_DUPLICATE.getCode(),
                        ErrorCodeConstants.USER_NAME_DUPLICATE.getMessage());
            } else if (!excludeId.equals(user.getId())) {
                throw new BizException(ErrorCodeConstants.USER_NAME_DUPLICATE.getCode(),
                        ErrorCodeConstants.USER_NAME_DUPLICATE.getMessage());
            }
        }

        // 检查手机号是否重复
        user = userGateway.getByMobile(mobile);
        if(user != null){
            if (excludeId != null && excludeId.equals(user.getId())) {
                throw new BizException(ErrorCodeConstants.USER_MOBILE_DUPLICATE.getCode(),
                        ErrorCodeConstants.USER_MOBILE_DUPLICATE.getMessage());
            }
        }
        if(!StrUtil.isEmpty(email)){
            // 检查邮箱是否重复
            user = userGateway.getByEmail(email);
            if(user != null){
                if (excludeId != null && excludeId.equals(user.getId())) {
                    throw new BizException(ErrorCodeConstants.USER_EMAIL_DUPLICATE.getCode(),
                            ErrorCodeConstants.USER_EMAIL_DUPLICATE.getMessage());
                }
            }
        }
    }

    /**
     * 对密码进行加密
     *
     * @param password 密码
     * @return 加密后的密码
     */
    public String encodePassword(String password) {
        return passwordEncoder.encode(password);
    }

    public void validatePassword(Long id, String oldPassword, String newPassword, String confirmPassword) {
        if(!newPassword.equals(confirmPassword)){
            throw new BizException(ErrorCodeConstants.USER_PASSWORD_CONFIRM_FAIL.getCode(),
                    ErrorCodeConstants.USER_PASSWORD_CONFIRM_FAIL.getMessage());
        }
        User user = userGateway.getById(id);
        if(user == null){
            throw new BizException(ErrorCodeConstants.USER_NOT_FOUND.getCode(),
                    ErrorCodeConstants.USER_NOT_FOUND.getMessage());
        }
        if (!passwordEncoder.matches(oldPassword, user.getPassword())) {
            throw new BizException(ErrorCodeConstants.USER_PASSWORD_COMPARE_FAIL.getCode(),
                    ErrorCodeConstants.USER_PASSWORD_COMPARE_FAIL.getMessage());
        }
        if (passwordEncoder.matches(newPassword, user.getPassword())) {
            throw new BizException(ErrorCodeConstants.USER_PASSWORD_SAME_AS_OLD.getCode(),
                    ErrorCodeConstants.USER_PASSWORD_SAME_AS_OLD.getMessage());
        }
    }

    public User authenticate(String username, String password) {
        User user = userGateway.getByUsername(username);
        if (user == null) {
            throw new BizException(ErrorCodeConstants.USER_NAME_NOT_FOUND.getCode());
        }
        if (!passwordEncoder.matches(password, user.getPassword())) {
            throw new BizException(ErrorCodeConstants.PASSWORD_INVALID.getCode(),
                    ErrorCodeConstants.PASSWORD_INVALID.getMessage());
        }
        // 让 User 自己校验状态
        user.isDisable();
        return user;
    }
}